
from PyQt4          import QtCore, QtGui
from Gui.MediaView  import MediaView
from Gui.MapView    import MapView
from Gui.HomeView   import HomeView
from Gui.PlayerView import PlayerView
from Resources      import Resources

class MainWindow( QtGui.QMainWindow ):
    sequenceNumber = 1
    windowList = []

    def __init__( self, fileName = None ):
        super( MainWindow, self ).__init__()

        self.init()
        if fileName:
            self.loadFile( fileName )
        else:
            self.setCurrentFile( '' )

    def closeEvent( self, event ):
        if self.maybeSave():
            self.writeSettings()
            event.accept()
        else:
            event.ignore()

    def newFile( self ):
        other = MainWindow()
        MainWindow.windowList.append( other )
        other.move( self.x() + 40, self.y() + 40 )
        other.show()

    def open( self ):
        self.homeView.openFile()

    def save( self ):
        if self.isUntitled:
            return self.saveAs()
        else:
            return self.saveFile( self.curFile )

    def saveAs( self ):
        fileName = QtGui.QFileDialog.getSaveFileName( self, self.tr( "Save As" ),
                self.curFile )
        if fileName.isEmpty():
            return False

        return self.saveFile( fileName )

    def about( self ):
        QtGui.QMessageBox.about( self, self.tr( "About SDI" ),
                self.tr( "The <b>SDI</b> example demonstrates how to write "
                        "single document interface applications using Qt." ) )

    def documentWasModified( self ):
        self.setWindowModified( True )

    #=============================================================
    # createDockWindows crea le viste ancorate
    #
    #=============================================================
    def createDockWindows( self ):

        #vista dei media disponibili'''
        dock = QtGui.QDockWidget( self.tr( "Media" ), self )
        dock.setAllowedAreas( QtCore.Qt.LeftDockWidgetArea )# | QtCore.Qt.BottomDockWidgetArea)
        self.mediaView = MediaView()
        dock.setWidget( self.mediaView )
        self.addDockWidget( QtCore.Qt.LeftDockWidgetArea, dock )

        # vista della casa'''
        dock = QtGui.QDockWidget( self.tr( "Home" ), self )
        dock.setAllowedAreas( QtCore.Qt.LeftDockWidgetArea | QtCore.Qt.RightDockWidgetArea )
        self.homeView = HomeView()
        dock.setWidget( self.homeView )
        self.addDockWidget( QtCore.Qt.LeftDockWidgetArea, dock )


        #vista del paler relativi ai media
        dock = QtGui.QDockWidget( self.tr( "Player" ), self )
        dock.setAllowedAreas( QtCore.Qt.BottomDockWidgetArea )
        self.playerView = PlayerView()
        dock.setWidget( self.playerView )
        self.addDockWidget( QtCore.Qt.BottomDockWidgetArea, dock )

    def init( self ):
        self.setAttribute( QtCore.Qt.WA_DeleteOnClose )
        self.isUntitled = True

        self.mapView = MapView()
        self.setCentralWidget( self.mapView )

        self.createDockWindows()

        self.createActions()
        self.createMenus()
        self.createToolBars()
        self.createStatusBar()
        self.createTrayIcon()

        self.readSettings()

       # self.textEdit.document().contentsChanged.connect(self.documentWasModified)

    def createActions( self ):


        self.newAct = QtGui.QAction( QtGui.QIcon( ":new.png" ),
                self.tr( "&New" ), self )
        self.newAct.setShortcut( QtGui.QKeySequence.New )
        self.newAct.setStatusTip( self.tr( "Create a new file" ) )
        self.newAct.triggered.connect( self.newFile )

        self.openAct = QtGui.QAction( QtGui.QIcon( ":open.png" ),
                self.tr( "&Open..." ), self )
        self.openAct.setShortcut( QtGui.QKeySequence.Open )
        self.openAct.setStatusTip( self.tr( "Open an existing file" ) )
        self.openAct.triggered.connect( self.open )

        self.saveAct = QtGui.QAction( QtGui.QIcon( ":save.png" ),
                self.tr( "&Save" ), self )
        self.saveAct.setShortcut( QtGui.QKeySequence.Save )
        self.saveAct.setStatusTip( self.tr( "Save the document to disk" ) )
        self.saveAct.triggered.connect( self.save )

        self.saveAsAct = QtGui.QAction( self.tr( "Save &As..." ), self )
        self.saveAsAct.setShortcut( QtGui.QKeySequence.SaveAs )
        self.saveAsAct.setStatusTip( self.tr( "Save the document under a new name" ) )
        self.saveAsAct.triggered.connect( self.saveAs )

        self.closeAct = QtGui.QAction( self.tr( "&Close" ), self )
        self.closeAct.setShortcut( self.tr( "Ctrl+W" ) )
        self.closeAct.setStatusTip( self.tr( "Close this window" ) )
        self.closeAct.triggered.connect( self.close )

        self.exitAct = QtGui.QAction( self.tr( "E&xit" ), self )
        self.exitAct.setShortcut( self.tr( "Ctrl+Q" ) )
        self.exitAct.setStatusTip( self.tr( "Exit the application" ) )
        self.exitAct.triggered.connect( QtGui.qApp.closeAllWindows )

        self.cutAct = QtGui.QAction( QtGui.QIcon( ":cut.png" ),
                self.tr( "Cu&t" ), self )
        self.cutAct.setShortcut( QtGui.QKeySequence.Cut )
        self.cutAct.setStatusTip( self.tr( "Cut the current selection's "
                                         "contents to the clipboard" ) )
   #     self.cutAct.triggered.connect(self.textEdit.cut)

        self.copyAct = QtGui.QAction( QtGui.QIcon( ":copy.png" ),
                self.tr( "&Copy" ), self )
        self.copyAct.setShortcut( QtGui.QKeySequence.Copy )
        self.copyAct.setStatusTip( self.tr( "Copy the current selection's "
                                          "contents to the clipboard" ) )
   #     self.copyAct.triggered.connect(self.textEdit.copy)

        self.pasteAct = QtGui.QAction( QtGui.QIcon( ":paste.png" ),
                self.tr( "&Paste" ), self )
        self.pasteAct.setShortcut( QtGui.QKeySequence.Paste )
        self.pasteAct.setStatusTip( self.tr( "Paste the clipboard's contents "
                                           "into the current selection" ) )
     #   self.pasteAct.triggered.connect(self.textEdit.paste)

        self.aboutAct = QtGui.QAction( self.tr( "&About" ), self )
        self.aboutAct.setStatusTip( self.tr( "Show the application's About box" ) )
        self.aboutAct.triggered.connect( self.about )

        self.aboutQtAct = QtGui.QAction( self.tr( "About &Qt" ), self )
        self.aboutQtAct.setStatusTip( self.tr( "Show the Qt library's About box" ) )
        self.aboutQtAct.triggered.connect( QtGui.qApp.aboutQt )

        self.cutAct.setEnabled( False )
        self.copyAct.setEnabled( False )
     #   self.textEdit.copyAvailable.connect(self.cutAct.setEnabled)
     #   self.textEdit.copyAvailable.connect(self.copyAct.setEnabled)

    def createMenus( self ):
        self.fileMenu = self.menuBar().addMenu( self.tr( "&File" ) )
        self.fileMenu.addAction( self.newAct )
        self.fileMenu.addAction( self.openAct )
        self.fileMenu.addAction( self.saveAct )
        self.fileMenu.addAction( self.saveAsAct )
        self.fileMenu.addSeparator()
        self.fileMenu.addAction( self.closeAct )
        self.fileMenu.addAction( self.exitAct )

        self.editMenu = self.menuBar().addMenu( self.tr( "&Edit" ) )
        self.editMenu.addAction( self.cutAct )
        self.editMenu.addAction( self.copyAct )
        self.editMenu.addAction( self.pasteAct )

        self.menuBar().addSeparator()

        self.helpMenu = self.menuBar().addMenu( self.tr( "&Help" ) )
        self.helpMenu.addAction( self.aboutAct )
        self.helpMenu.addAction( self.aboutQtAct )

    def createToolBars( self ):
        self.fileToolBar = self.addToolBar( self.tr( "File" ) )
        self.fileToolBar.addAction( self.newAct )
        self.fileToolBar.addAction( self.openAct )
        self.fileToolBar.addAction( self.saveAct )

        self.editToolBar = self.addToolBar( self.tr( "Edit" ) )
        self.editToolBar.addAction( self.cutAct )
        self.editToolBar.addAction( self.copyAct )
        self.editToolBar.addAction( self.pasteAct )


    def createStatusBar( self ):
        self.statusBar().showMessage( self.tr( "Ready" ) )

    def readSettings( self ):
        settings = QtCore.QSettings( "Trolltech", "SDI Example" )
        pos = settings.value( "pos", QtCore.QPoint( 200, 200 ) )
        size = settings.value( "size", QtCore.QSize( 400, 400 ) )
        self.move( pos )
        self.resize( size )

    def writeSettings( self ):
        settings = QtCore.QSettings( "Trolltech", "SDI Example" )
        settings.setValue( "pos", self.pos() )
        settings.setValue( "size", self.size() )

    def maybeSave( self ):
        return True
#===============================================================================
#        if self.textEdit.document().isModified():
#            ret = QtGui.QMessageBox.warning(self, self.tr("SDI"),
#                    self.tr("The document has been modified.\n"
#                            "Do you want to save your changes?"),
#                    QtGui.QMessageBox.Save | QtGui.QMessageBox.Discard |
#                    QtGui.QMessageBox.Cancel)
#            if ret == QtGui.QMessageBox.Save:
#                return self.save()
#            elif ret == QtGui.QMessageBox.Cancel:
#                return False
#        return True
#===============================================================================

    def loadFile( self, fileName ):
        file = QtCore.QFile( fileName )
        if not file.open( QtCore.QFile.ReadOnly | QtCore.QFile.Text ):
            QtGui.QMessageBox.warning( self, self.tr( "SDI" ),
                    self.tr( "Cannot read file %1:\n%2." ).arg( fileName ).arg( file.errorString() ) )
            return

        instr = QtCore.QTextStream( file )
        QtGui.QApplication.setOverrideCursor( QtCore.Qt.WaitCursor )
        #self.textEdit.setPlainText(instr.readAll())
        QtGui.QApplication.restoreOverrideCursor()

        self.setCurrentFile( fileName )
        self.statusBar().showMessage( self.tr( "File loaded" ), 2000 )

    def saveFile( self, fileName ):
        file = QtCore.QFile( fileName )
        if not file.open( QtCore.QFile.WriteOnly | QtCore.QFile.Text ):
            QtGui.QMessageBox.warning( self, self.tr( "SDI" ),
                    self.tr( "Cannot write file %1:\n%2." ).arg( fileName ).arg( file.errorString() ) )
            return False

        outstr = QtCore.QTextStream( file )
        QtGui.QApplication.setOverrideCursor( QtCore.Qt.WaitCursor )
     #   outstr << self.textEdit.toPlainText()
        QtGui.QApplication.restoreOverrideCursor()

        self.setCurrentFile( fileName )
        self.statusBar().showMessage( self.tr( "File saved" ), 2000 )
        return True

    def setCurrentFile( self, fileName ):
        self.isUntitled = not fileName
        if self.isUntitled:
            self.curFile = "document%d.txt" % MainWindow.sequenceNumber
            MainWindow.sequenceNumber += 1
        else:
            self.curFile = QtCore.QFileInfo( fileName ).canonicalFilePath()


        self.setWindowModified( False )

        self.setWindowTitle( "%s[*] - SDI" % self.strippedName( self.curFile ) )

    def strippedName( self, fullFileName ):
        return QtCore.QFileInfo( fullFileName ).fileName()

    def findMainWindow( self, fileName ):
        canonicalFilePath = QtCore.QFileInfo( fileName ).canonicalFilePath()

        for widget in QtGui.qApp.topLevelWidgets():
            if isinstance( widget, MainWindow ) and widget.curFile == canonicalFilePath:
                return widget

        return None

    def createTrayIcon( self ):
        'menu dell icona tray'
        self.trayIconMenu = QtGui.QMenu( self )
        self.trayIconMenu.addAction( self.openAct )
        self.trayIconMenu.addSeparator()
        self.trayIconMenu.addAction( self.exitAct )

        icon = QtGui.QIcon( ':video.png' )
        self.setWindowIcon( icon )
        self.trayIcon = QtGui.QSystemTrayIcon( self )
        self.trayIcon.setContextMenu( self.trayIconMenu )
        self.trayIcon.setToolTip( "prova" )
        self.trayIcon.setIcon( icon )
        self.trayIcon.show()





if __name__ == '__main__':

    import sys

    app = QtGui.QApplication( sys.argv )
    mainWin = MainWindow()
    mainWin.show()
    sys.exit( app.exec_() )
